import 'package:good_good_study/common/index.dart';
import 'package:good_good_study/entity/home_list_entity.dart' as HomeArticle;
import 'package:good_good_study/entity/user_login_entity.dart' as UserLogin;
import 'package:good_good_study/entity/collect_list_entity.dart' as Collect;

class Http {
  static const String BASE_URL = "https://www.wanandroid.com/";

  static HttpManager _httpProxyIns;

  static _createHttpProxy() {
    _httpProxyIns = HttpManager(BASE_URL,
        CODE_NAME: "errorCode", CODE_SUCCESS_VALUE: 0, MSG_NAME: "errorMsg");
  }

  ///用户注册
  static Stream<UserLogin.UserLoginEntity> userReg(name, pwd, rePwd) {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.post(
        "user/register", (resMap) => UserLogin.UserLoginEntity.fromJson(resMap),
        queryParams: Map()
          ..["username"] = name
          ..["password"] = pwd
          ..["repassword"] = rePwd);
  }

  ///用户登录
  static Stream<UserLogin.UserLoginEntity> userLogin(name, pwd) {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.post(
        "user/login", (resMap) => UserLogin.UserLoginEntity.fromJson(resMap),
        queryParams: Map()
          ..["username"] = name
          ..["password"] = pwd);
  }

  ///用户退出登录
  static Stream<UserLogin.UserLoginEntity> userLogout() {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.get("user/logout/json", (resMap) => resMap);
  }

  ///首页文章列表
  static Stream<HomeArticle.HomeListEntity> getHomeList({page = 0}) {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.get("article/list/$page/json",
        (resMap) => HomeArticle.HomeListEntity.fromJson(resMap));
  }

  ///收藏文章列表
  static Stream<Collect.CollectListEntity> collectList({page = 0}) {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.get(
        "lg/collect/list/$page/json", (resMap) => Collect.CollectListEntity.fromJson(resMap));
  }

  ///收藏站内文章
  static Stream<dynamic> collect(id) {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.post("lg/collect/$id/json", (resMap) => resMap);
  }

  ///取消收藏站内文章
  static Stream<dynamic> cancelCollect(id) {
    _httpProxyIns ?? _createHttpProxy();
    return _httpProxyIns?.post(
        "lg/uncollect_originId/$id/json", (resMap) => resMap);
  }
}
